package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SiyuProduct;
import com.ruoyi.project.system.domain.SiyuUserPrice;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.vo.ProductPriceVo;
import com.ruoyi.project.system.domain.vo.UserProduct;
import com.ruoyi.project.system.domain.vo.UserProductPriceVo;
import com.ruoyi.project.system.mapper.SiyuProductMapper;
import com.ruoyi.project.system.mapper.SiyuUserPriceMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.system.service.ISiyuProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查询产品Controller
 *
 */
@RestController
@RequestMapping("/system/product")
public class SiyuProductController extends BaseController {
    @Autowired
    private ISiyuProductService siyuProductService;

    @Autowired
    private SiyuProductMapper siyuProductMapper;

    @Autowired
    private SysUserMapper sup;

    @Autowired
    private SiyuUserPriceMapper supm;

    /**
     * 修改价格
     */
    @GetMapping("/setPrice")
    public AjaxResult list(SiyuUserPrice siyuUserPrice) {
        SiyuUserPrice userPrice = supm.selectPriceByUIAadPI(siyuUserPrice.getUserId().toString(), siyuUserPrice.getProductId().toString());
        if (userPrice != null && userPrice.getPrice() != null) {
            return toAjax(supm.updateSiyuUserPrice(siyuUserPrice));
        } else {
            return toAjax(supm.insertSiyuUserPrice(new SiyuUserPrice(siyuUserPrice)));
        }
    }

    /**
     * 用户Tabs
     */
    @GetMapping("/userQueryItem")
    public AjaxResult userQueryItem(@NotNull Integer sort) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        UserProductPriceVo UPPV = new UserProductPriceVo(user);
        List<SiyuProduct> products = siyuProductMapper.selectSiyuProductListBySort(sort);
        List<Integer> allProductId = products.stream().map(SiyuProduct::getId).collect(Collectors.toList());
        List<SiyuUserPrice> userPrices = supm.selectPricesByIds(allProductId, user.getUserId());
        List<Integer> partProductId = userPrices.stream().map(SiyuUserPrice::getProductId).collect(Collectors.toList());
        for (int i = 0; i < allProductId.size(); i++) {
            int index = partProductId.indexOf(allProductId.get(i));
            if (index == -1) {
                UPPV.getProductPriceVos().add(new ProductPriceVo(products.get(i)));
            } else {
                UPPV.getProductPriceVos().add(new ProductPriceVo(products.get(i), userPrices.get(index)));
            }
        }
        return success(UPPV);
    }

    /**
     * 查询用户产品售价
     */
    @GetMapping("/userPrice")
    public TableDataInfo usersPrices(UserProduct UP) {
        startPage();
        List<SysUser> users = sup.selectUserList(new SysUser(UP.getUserName()));
        List<SiyuProduct> products = siyuProductService.selectSiyuProductList(new SiyuProduct(UP.getName()));
        List<Integer> allProductId = products.stream().map(SiyuProduct::getId).collect(Collectors.toList());
        List<UserProductPriceVo> UPPVS = new ArrayList<>();
        for (SysUser user : users) {
            UserProductPriceVo UPPV = new UserProductPriceVo(user);
            List<SiyuUserPrice> userPrices = supm.selectPricesByIds(allProductId, user.getUserId());
            List<Integer> partProductId = userPrices.stream().map(SiyuUserPrice::getProductId).collect(Collectors.toList());
            for (int i = 0; i < allProductId.size(); i++) {
                int index = partProductId.indexOf(allProductId.get(i));
                if (index == -1) {
                    UPPV.getProductPriceVos().add(new ProductPriceVo(products.get(i)));
                } else {
                    UPPV.getProductPriceVos().add(new ProductPriceVo(products.get(i), userPrices.get(index)));
                }
            }
            UPPVS.add(UPPV);
        }
        return getDataTable(UPPVS);
    }

    /**
     * 查询查询产品列表
     */
    @PreAuthorize("@ss.hasPermi('system:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(SiyuProduct siyuProduct) {
        startPage();
        List<SiyuProduct> list = siyuProductService.selectSiyuProductList(siyuProduct);
        return getDataTable(list);
    }

    /**
     * 获取查询产品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(siyuProductService.selectSiyuProductById(id));
    }

    /**
     * 修改查询产品
     */
    @PreAuthorize("@ss.hasPermi('system:product:edit')")
    @Log(title = "查询产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SiyuProduct siyuProduct) {
        return toAjax(siyuProductService.updateSiyuProduct(siyuProduct));
    }

}
